package com.assumejson.aop.config;

import com.assumejson.aop.advice.ZhouyuAfterReturningAdvice;
import com.assumejson.aop.service.OrderService;
import com.assumejson.aop.service.UserService;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.context.annotation.*;

/**
 * @author Mr. Li
 * @version 1.0
 * @since 1.0 2021/12/9 10:27
 */
@Configuration
//@ComponentScan(value = "com.assumejson.aop",
//		includeFilters = {@ComponentScan.Filter(
//				type = FilterType.ASSIGNABLE_TYPE,
//				classes = UserService.class
//		)})
//@EnableAspectJAutoProxy()
@ComponentScan("com.assumejson.aop")
@Import(DefaultAdvisorAutoProxyCreator.class)
public class AppConfig {

	@Bean
	public OrderService orderService() {
		return new OrderService();
	}

	// Spring AOP 的 核心 DefaultPointcutAdvisor
	@Bean
	public DefaultPointcutAdvisor defaultPointcutAdvisor() {
		NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
		pointcut.addMethodName("test");
		DefaultPointcutAdvisor defaultPointcutAdvisor = new DefaultPointcutAdvisor();
		defaultPointcutAdvisor.setPointcut(pointcut);
		defaultPointcutAdvisor.setAdvice(new ZhouyuAfterReturningAdvice());
		return defaultPointcutAdvisor;
	}

	// 将DefaultPointcutAdvisor 注入到Spring 容器中

	/**
	 * 该bean 实现beanPostProcessor
	 *
	 * 还有另外一种实现方式就是通过@Import方式将该bena 导入到Spring 容器中
	 * @return
	 */
//	@Bean
//	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
//		return new DefaultAdvisorAutoProxyCreator();
//	}
}
